/*------------------------------------------------------------------------------
 * PROJ : PrototypeWithSpring
 * NAME : sculove.test.hlpParser.KeicHlpParser.java
 *        
 * Copyright 2009 sculove All rights reserved
 *------------------------------------------------------------------------------
*/
package sculove.test.parser.hlp;

import java.io.File;
import java.util.HashSet;
import java.util.Iterator;

import jxl.CellView;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import sculove.spring.framework.util.SStringUtil;

/**
 * <pre>
 * sculove.test.hlpParser.KeicHlpParser.java
 * TODO
 * 
 * </pre>
 * @date : 2009. 10. 19. ���� 9:30:36
 * @version :
 * @author : sculove
 */
public class KeicHlpParser {
	private final Log logger = LogFactory.getLog(this.getClass());
	
	private HashSet<String> hlp = new HashSet<String>();
	
	
	public static void main(String[] args) {
		KeicHlpParser hlpParser = new KeicHlpParser();
		File baseDir = new File("E:\\keicDev\\view\\sculove_view\\KEIC_SRC\\keic");
		
		System.out.println("--Start to parse Hlp...");
        if(baseDir.isDirectory()) {
        	hlpParser.searchHlp(baseDir);
        }
        System.out.println("--End to parse Hlp...");
        System.out.println("--Start to write excel...");
        hlpParser.writeExcel("C:/�¶��ε��� ����߸��.xls");
        System.out.println("--End to write excel...");
	}
		
    /**
     * @param target
     */
    public void searchHlp(File target) {
        if(target.isDirectory()) {    // Directory�� ���
            String[] child = target.list();
            for(int i = 0; i < child.length; i++){
            	searchHlp(new File( target.getAbsolutePath() + "\\" + child[i]));
            }
        } else {    // File�� ���
            if("xrw".equalsIgnoreCase(SStringUtil.unqualify(target.getAbsolutePath(), '.')) 
            		&& (target.getName().length() == 14)
            		&& target.getName().charAt(7) != 'I'
    				&& target.getName().charAt(7) != 'P'            			
            ) {
            	
//            	logger.info("�ڣ� " + KeicHlpParser.parsingFileName(target.getName()));
            	File hlpFile = new File(parsingFileName(target.getName()));
//            	if(!hlpFile.isFile()) {	// ��� ��� ��
////            		logger.info("�ڣ� " + target.getAbsolutePath() + " : " + hlpFile.isFile());
//            		hlp.add(hlpFile.getAbsolutePath());
//            	}
            	
            	if(hlpFile.isFile()) {	// ��� ��� ��
            		if(isReayDoc(hlpFile)) {
            			hlp.add(hlpFile.getAbsolutePath());	
            		}
            	}
            }
        }
    }
    
    private String parsingFileName(String file) {
    	String hlpfilename = "";
    	String path = "e:\\Test\\";
    	if(file.length() == 14) { 
    		file = file.substring(0, file.indexOf("."));
    		path += file.substring(0,3).toLowerCase() + "\\" + file.substring(0,4).toLowerCase();
    		hlpfilename = path + "\\" + file + ".htm";
    	}
    	return hlpfilename;
    }
    
    private boolean isReayDoc(File file) {
//    	BufferedReader br = null;
//    	boolean isReady = false;
    	
//    	try {
//        	br = new BufferedReader(new FileReader(file));
//        	
//        	String lineContent;
//        	while ( (lineContent = br.readLine()) != null ) {
//        		if( lineContent.indexOf("�¶��� ���� �غ����Դϴ�") != -1) {			// ����
//        			isReady = true;
//        			break;
//        		} 
//        	}		
//		} catch (Exception e) {
//		} finally {
//			try { if(br != null) { br.close(); br = null; } } catch(Exception be) {}
//		}
    	if(file.length() == 4926)
    		return true;
    	else return false;
    }
    
    
    /**
     * Excel �����
     * @param filepath
     */
    private void writeExcel(String filepath) {
        WritableWorkbook workbook = null;
        WritableSheet sheet = null;
        File excelFile = new File(filepath);
        
        try{
            workbook = Workbook.createWorkbook(excelFile);
            sheet = workbook.createSheet("�̵�� �¶��� ���� ���", 0);
            
            // �÷� Auto size ����
            CellView autoSizeCellView = new CellView();
            autoSizeCellView.setAutosize(true);
            sheet.setColumnView(0, autoSizeCellView);
            sheet.setColumnView(1, autoSizeCellView);
            sheet.setColumnView(2, autoSizeCellView);
            sheet.setColumnView(3, autoSizeCellView);
            
            sheet.addCell(new Label(0,0,"������"));
            sheet.addCell(new Label(1,0,"�����ڵ�"));
            sheet.addCell(new Label(2,0,"��������"));
            
            int row = 1;
            Iterator<String> iter = hlp.iterator();
            while(iter.hasNext()) {
                String key = iter.next();
                String appCd = key.substring(key.indexOf("e:\\Test\\")+8,key.indexOf("e:\\Test\\")+11);
                String appNm ="";
                if(appCd.equals("Keic")) appNm="����"; 
                else if(appCd.equals("itc")) appNm="ITSM";
                else if(appCd.equals("mac")) appNm="ȸ��";
                else if(appCd.equals("mhc")) appNm="�λ�";
                else if(appCd.equals("muc")) appNm="��������";
                else if(appCd.equals("rac")) appNm="�μ�";
                else if(appCd.equals("rcc")) appNm="����";
                else if(appCd.equals("rec")) appNm="���̹����⺸����";
                else if(appCd.equals("ric")) appNm="�ſ�����";
                else if(appCd.equals("rmc")) appNm="��ǰ";
                else if(appCd.equals("rrc")) appNm="���İ�";
                else if(appCd.equals("rsc")) appNm="�?����";
                else if(appCd.equals("ruc")) appNm="��������";
                else if(appCd.equals("zzz")) appNm="�������";
            	
            	sheet.addCell(new Label(0,row, appNm));
                sheet.addCell(new Label(1,row, appCd));
                sheet.addCell(new Label(2,row, key.substring(key.indexOf("y:\\Test\\")+8)));
                row++;
            }
        }catch(Exception e){
           e.printStackTrace();
        } finally {
            try{
                if(workbook != null) workbook.write();
                if(workbook != null) workbook.close();   
            }catch(Exception e2){}
        }
    }       
}
